var t = require("../../api.js"), a = (require("../../utils.js"), getApp()), e = !1;
var h = require("../../api.js"), i = require("../../area-picker/area-picker.js"), app = getApp();
Page({
    data: {
        page: 1,
        page_count: 1,
        longitude: "",
        latitude: "",
        score: [ 1, 2, 3, 4, 5 ],
        keyword: "",
        myAddress:'',
        hidden_shop:0,
        district: null,
        isnot_show_district: 1
    },
    onLoad: function(t) {
      var thst = this;
      var d = this;
      d.getDistrictData(function (t) {
        i.init({
          page: d,
          data: t
        });
      }),
      a.request({
        url: h.default.login_style,
        method: "POST",
        success: function (t) {
          if (t == 0) {
            thst.data.hidden_shop =1;
            thst.setData({
              hidden_shop:1
            })
          } 
        },
      })
      a.pageOnLoad(this, t);
      var e = this, o = t.user_id;
      var thst = this;
      a.loginBindParent({
          parent_id: o
      }), thst.dingwei(1);
      this.themeColor()
      this.titleName()
    },
    getDistrictData: function (i) {
      var a = wx.getStorageSync("district");
      if (!a) return wx.showLoading({
        title: "正在加载",
        mask: !0
      }), void app.request({
        url: t.default.district,
        success: function (t) {
          wx.hideLoading(), 0 == t.code && (a = t.data, wx.setStorageSync("district", a),
            i(a));
        }
      });
      i(a);
    },
    themeColor: function () {
      var cl = wx.getStorageSync('_navigation_bar_color').themeColor;
      var cl_b = !cl || cl == '#ffffff' ? 'ff4544' : cl;
      var color_rgb = this.hexToRgb(cl_b);
      var back_g = 'rgba(' + color_rgb.r + ',' + color_rgb.g + ',' + color_rgb.b + ',.1)'
      this.setData({
        all_color: cl ? cl : '#ffffff',
        back_g: back_g
        // back_end: back_end
      });
      wx.setNavigationBarColor({
        frontColor: cl != '#ffffff' ? '#ffffff' : '#000000',
        backgroundColor: cl_b,
        animation: {
          duration: 0,
          timingFunc: 'easeIn'
        }
      })
    },
    titleName : function(){
      var n = this;
      wx.setNavigationBarTitle({ 
        title:n.data.store.sq_name
      })
    },
    hexToRgb: function (hex) {
      var result = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(hex);
      return result ? {
        r: parseInt(result[1], 16),
        g: parseInt(result[2], 16),
        b: parseInt(result[3], 16)
      } : null;
    },
    onReady: function() {},
    onShow: function() {
        a.pageOnShow(this);
    },
  onAreaPickerConfirm: function (t) {
    this.setData({
      district: {
        province: {
          id: t[0].id,
          name: t[0].name
        },
        city: {
          id: t[1].id,
          name: t[1].name
        },
        district: {
          id: t[2].id,
          name: t[2].name
        }
      }
    });
    this.search();
  },
    dingwei: function(status = 0) {
        var t = this;
        
        if (status == 1) {
          wx.getLocation({
            success: function (res) {
              t.setData({
                longitude: res.longitude,
                latitude: res.latitude,
              })
              t.locationContent();
            },
            fail: function (res) {
              app.getauth({
                content: "需要获取您的地理位置授权，请到小程序设置中打开授权",
                success: function(e) {
                    e && (e.authSetting["scope.userLocation"] ? t.dingwei() : wx.showToast({
                        title: "您取消了授权",
                        image: "/images/icon-warning.png"
                    }));
                }
              });
            },
            complete: function() {
              t.loadData();
            }
          })
        }else {
          wx.chooseLocation({
            success: function(e) {
                t.setData({
                  longitude: e.longitude,
                  latitude: e.latitude,
                  myAddress: e.address
                })
            },
            fail: function(a) {
                app.getauth({
                    content: "需要获取您的地理位置授权，请到小程序设置中打开授权",
                    success: function(e) {
                        e && (e.authSetting["scope.userLocation"] ? t.dingwei() : wx.showToast({
                            title: "您取消了授权",
                            image: "/images/icon-warning.png"
                        }));
                    }
                });
            },
            complete: function() {
                t.loadData();
            }
        });
        }
        
    },
    loadData: function() {
        var e = this;
        wx.showLoading({
            title: "加载中"
        }), a.request({
            url: t.default.shop_list,
            method: "GET",
            data: {
                longitude: e.data.longitude,
                latitude: e.data.latitude,
                city_id: e.data.district ? e.data.district.city.id : 0,
            },
            success: function(t) {
              //t.data.shop = t.data.shop ? t.data.shop : wx.getStorageSync("address_info");
              if (t.data.shop) {
                t.data.shop.peisong_type = t.data.shop.peisong_type ? JSON.parse(t.data.shop.peisong_type) : t.data.shop.peisong_type;
              }
                0 == t.code && e.setData(t.data);
              if (e.data.hidden_shop == 1) {
                var shop_info = wx.getStorageSync('address_info');
                if (shop_info && shop_info.peisong_type) {
                  shop_info.peisong_type = shop_info.peisong_type != '' && typeof(shop_info.peisong_type) == 'string' ? JSON.parse(shop_info.peisong_type) : shop_info.peisong_type;
                }
               
                e.setData({
                  shop: shop_info
                })
              }
            },
            fail: function(t) {},
            complete: function() {
                wx.hideLoading();
            }
        });
        //e.locationContent();
    },
    ChoiceShop:function(con){
        var index = con.currentTarget.dataset.index;
        var pages = getCurrentPages();
        var url = pages.length >= 2 ? pages[pages.length - 2].__route__ : '';
        var that = this;
        wx.setStorage({
          key: "address_info",
          data: {
            nickname: that.data.list[index].nickname,
            name: that.data.list[index].name,
            address: that.data.list[index].address,
            logo: that.data.list[index].pic_url,
            shop_id: that.data.list[index].id,
            peisong_type: that.data.list[index].peisong_type,
            mobile: that.data.list[index].mobile
          }
        })
        if(this.data.hidden_shop == 1){
          if(url == 'pages/goods/goods') {
            wx.navigateBack()
          } else {
            wx.reLaunch({
              url: "/pages/index/index",
            })
          }
          return false;
        }else {
            var id = con.currentTarget.id;
            var th = this;
            a.request({
                url: getApp().api.default.index_position,
                data: {
                  tz_id: id,
                  longitude: th.data.longitude,
                  latitude: th.data.latitude,
                  is_update: 1
                },
                method: "POST",
                success: function (t) {
                  if (url == 'pages/new-order-submit/new-order-submit'
                    || url == 'miaosha/order-submit/order-submit'
                    || url == 'pt/order-submit/order-submit'
                  ) {
                    wx.setStorageSync("picker_order", th.data.list[index]), wx.navigateBack();
                    return false;
                  } else if(url == 'pages/goods/goods') {
                    wx.navigateBack()
                  } else {
                    wx.reLaunch({
                      url: "/pages/index/index",
                    })
                    return false;
                  }
                },
            })
        }
    },
    onHide: function() {},
    onUnload: function() {},
    onPullDownRefresh: function() {
        var t = this;
        t.setData({
            keyword: "",
            district:null,
            page: 1
        }), wx.getLocation({
            success: function(a) {
                t.setData({
                    longitude: a.longitude,
                    latitude: a.latitude
                });
            },
            complete: function() {
                t.loadData(), wx.stopPullDownRefresh();
            }
        });
    },
    onReachBottom: function() {
        var t = this;
        t.data.page >= t.data.page_count || t.loadMoreData();
    },
    loadMoreData: function() {
        var o = this, n = o.data.page;
        e || (e = !0, wx.showLoading({
            title: "加载中",
        }), a.request({
            url: t.default.shop_list,
            method: "GET",
            data: {
                page: n,
                longitude: o.data.longitude,
                latitude: o.data.latitude,
                city_id: o.data.district ? o.data.district.city.id : 0,
            },
            success: function(t) {
                if (0 == t.code) {
                    var a = o.data.list.concat(t.data.list);
                    o.setData({
                        list: a,
                        page_count: t.data.page_count,
                        row_count: t.data.row_count,
                        page: n + 1
                    });
                }
            },
            complete: function() {
                wx.hideLoading(), e = !1;
            }
        }));
    },
    Relocation:function(){
        var th = this;
        th.dingwei();
    },
    locationContent:function (){
        var o = this;
        a.request({
            url: h.default.location_content,
            method: "GET",
            data: {
                longitude: o.data.longitude,
                latitude: o.data.latitude
            },
            success: function(t) {
                o.setData({
                    myAddress:t,
                })
            },
            fail: function(t) {
                o.setData({
                    myAddress:"定位失败",
                })
            },
        });
    },
    goto: function(t) {
        var e = this;
        "undefined" != typeof my ? e.location(t) : wx.getSetting({
            success: function(o) {
                o.authSetting["scope.userLocation"] ? e.location(t) : a.getauth({
                    content: "需要获取您的地理位置授权，请到小程序设置中打开授权！",
                    cancel: !1,
                    success: function(a) {
                        a.authSetting["scope.userLocation"] && e.location(t);
                    }
                });
            }
        });
    },
    location: function(t) {
        var a = this, e = t.currentTarget.dataset.index, o = a.data.list;
        wx.openLocation({
            latitude: parseFloat(o[e].latitude),
            longitude: parseFloat(o[e].longitude),
            name: o[e].name,
            address: o[e].address
        });
    },
    inputFocus: function(t) {
        this.setData({
            show: !0
        });
    },
    inputBlur: function(t) {
        this.setData({
            show: !1
        });
    },
    inputConfirm: function(t) {
        this.search();
    },
    input: function(t) {
        this.setData({
            keyword: t.detail.value
        });
    },
    search: function(e) {
        var o = this;
        wx.showLoading({
            title: "搜索中"
        });
        var s = o.data.district;
        s || (s = { 
          district: {
            id: ""
          }
        }),
         a.request({
            url: t.default.shop_list,
            method: "GET",
            data: {
                keyword: o.data.keyword,
                city_id: o.data.district ? o.data.district.city.id : 0,
                longitude: o.data.longitude,
                latitude: o.data.latitude
            },
          success: function (t) {
            t.data
                0 == t.code && o.setData({
                  list:t.data.list,
                  page_count: t.data.page_count,
                  row_count: t.data.row_count
                });
            },
            complete: function() {
                wx.hideLoading();
            }
        });
    },
    go: function(t) {
        var a = this, e = t.currentTarget.dataset.index, o = a.data.list;
        wx.navigateTo({
            url: "/pages/shop-detail/shop-detail?shop_id=" + o[e].id
        });
    },
    navigatorClick: function(t) {
        var a = t.currentTarget.dataset.open_type, e = t.currentTarget.dataset.url;
        return "wxapp" != a || (e = function(t) {
            var a = /([^&=]+)=([\w\W]*?)(&|$|#)/g, e = /^[^\?]+\?([\w\W]+)$/.exec(t), o = {};
            if (e && e[1]) for (var n, i = e[1]; null != (n = a.exec(i)); ) o[n[1]] = n[2];
            return o;
        }(e), e.path = e.path ? decodeURIComponent(e.path) : "", wx.navigateToMiniProgram({
            appId: e.appId,
            path: e.path,
            complete: function(t) {}
        }), !1);
    },
    onShareAppMessage: function(t) {
        return {
            path: "/pages/shop/shop?user_id=" + wx.getStorageSync("user_info").id
        };
    }
});